package com.yunchen.BooksManagementSystem.Filter;

import com.yunchen.BooksManagementSystem.login.entity.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//@WebFilter(filterName = "LoginFilter",urlPatterns = "/*")
public class LoginFilter implements javax.servlet.Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse reps = (HttpServletResponse)response;
        User userSession = (User)req.getSession().getAttribute("userSession");
        if (userSession==null) {
            reps.sendRedirect("/LoginServlet");
        } else {
            chain.doFilter(request,response);
        }
    }

    @Override
    public void destroy() {

    }
}
